<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
*/
session_start();
use Unirest\Request AS API;

$app->get('/', function(){

	if(!empty($_GET["code"])){
        $body   = [
            "client_id"     => env("CLIENT_ID"),
            "client_secret" => env("CLIENT_SECRET"),
            "grant_type"    => "authorization_code",
            "code"          => $_GET["code"]
        ];

		$tokenResponse = API::post(env("OAUTH_URL")."/token",[],$body);
		if($tokenResponse->code!=200 || $tokenResponse->body->error == true)
            return (array)$tokenResponse->body;

        $result = $tokenResponse->body->result;

        $_SESSION["user_id"] = $result->user_id;
        $_SESSION["authorization"] = $result->token_type." ".$result->access_token;

        if(!empty($_GET["state"]))
        	return redirect(urldecode($_GET["state"]));
	}

	var_dump($_SESSION);
});

$app->get('/user/zfinfo', function(){
	if(empty($_SESSION["user_id"]) || empty($_SESSION["authorization"])){
		$state = urlencode("http://".env("APP_URL")."/user/zfinfo");
		$redirectUrl  = env("OAUTH_URL")."/auth?client_id=".env("CLIENT_ID")."&response_type=code&state=$state";
		return redirect($redirectUrl);
	}

	$userId    = $_SESSION["user_id"];
	$systemCat = "edu";

	$header = [
		"host"          => "campus",
		"authorization" => $_SESSION["authorization"]
	];

	$userIntroResponse = API::post(env("PTIME_API")."/user/$userId/system_cat/edu/table",$header);

	if(!isset($userIntroResponse->body->error) || $userIntroResponse->body->error == true){
		$state = urlencode("http://".env("APP_URL")."/user/zfinfo");
		$redirectUrl  = env("CAMPUS_OAUTH_URL")."/auth?state=$state&user_id=$userId&system_cat=$systemCat";
		return redirect($redirectUrl);
	}

	return (array)$userIntroResponse->body;

});

$app->get('/wechat','Oauth2Controller@wechat');
$app->post('/wechat','Oauth2Controller@wechat');

$app->get('/user/info','UserController@info');
$app->get('/logout', 'UserController@logout');

$app->get('/user/{uid}/verify','UserController@verify');


$app->get('/oauth2/auth','Oauth2Controller@authPage');
$app->get('/oauth2/auth2','Oauth2Controller@auth2Page');
$app->post('/oauth2/auth','Oauth2Controller@auth');
$app->post('/oauth2/token','Oauth2Controller@token');

$app->get('/campus/oauth2/auth','CampusOauth2Controller@authPage');

$app->post('/file','ApiController@upload');

$app->get('/4aee9a9cc027374ebf28826b12e47e32', 'DataController@base');

$app->post('/restapi',function(){
	$input = $_POST;
	if(empty($input["host"]))
		return "no host";

	$method = isset($input["method"])?$input["method"]:"GET";
    $path   = isset($input["path"])?$input["path"]:"/";
    $query  = isset($input["query"])?$input["query"]:"";
    $header = [
    	"Host"          => $input["host"],
    	"Authorization" => "Bearer ".env("ACCESS_TOKEN")
    ];

    if(strtoupper($method) == "PUT")
    	$query = \Unirest\Request\Body::form($query);

    $response = API::$method(env("PTIME_API").$path,$header,$query);
    return (array)$response->body;
});
